import {
	jwtInstance,
	robot
} from './index.js'
import {
	getTokenUser
} from '../lib/utils.js'

export const login = (username, password, code) => {
	return jwtInstance.request({
		url: '/login',
		method: 'get',
		params: {
			username: username,
			password: password,
			code: code
		}
	})
}

//获取用户信息接口(个人中心)
export const getUserInfo = (code) => {
	return jwtInstance.request({
		url: '/user/user',
		method: 'get',
		params: {
			code: getTokenUser()
		}
	})
}

export const getAllCollege = () => {
	return jwtInstance.request({
		url: '/college/allcolleges',
		method: 'get',
	})
}

//JWT令牌刷新接口
export const refreshToken = () => {
	return jwtInstance.request({
		url: '/refreshtoken',
		method: 'get'
	})
}

export const getUsers = (collegeid, categoryid, classid, code, name, sex, identity, current, size) => {
	return jwtInstance.request({
		url: '/user/users',
		method: 'get',
		params: {
			collegeid: collegeid,
			categoryid: categoryid,
			classid: classid,
			code: code,
			name: name,
			sex: sex,
			identity: identity,
			current: current,
			size: size
		}
	})
}


export const addUser = (formData) => {
	return jwtInstance.request({
		url: '/user/users',
		method: 'post',
		data: {
			code: formData.code,
			name: formData.name,
			collegeid: formData.collegeid,
			categoryid: formData.categoryid,
			classid: formData.classid,
			sex: formData.sex,
			identity: formData.identity,
			password: formData.password,
		}
	})
}
export const addClassroom = (formData) => {
	return jwtInstance.request({
		url: '/classroom/classrooms',
		method: 'post',
		data: {
			code: formData.code,
			collegeid: formData.collegeid,
			type: formData.type,
			size: formData.size,
		}
	})
}

export const askClassroom = (formData) => {
	return jwtInstance.request({
		url: '/ask/asks',
		method: 'post',
		data: {
			uid: formData.uid,
			identity: formData.identity,
			cid: formData.cid,
			reason: formData.reason,
			email: formData.email,
			time1: formData.time1,
			time2: formData.time2,
			time3: formData.time3,
			time4: formData.time4,
			time5: formData.time5,
			time6: formData.time6,
			date: formData.date,
		}
	})
}

export const getClassroom = (collegeid, type, size, time1, time2, time3, time4, time5, time6, date, current,
	size1) => {
	return jwtInstance.request({
		url: 'classroom/classrooms',
		method: 'get',
		params: {
			collegeid: collegeid,
			type: type,
			size: size,
			time1: time1,
			time2: time2,
			time3: time3,
			time4: time4,
			time5: time5,
			time6: time6,
			date: date,
			current: current,
			size1: size1
		}
	})
}


//根据登录用户名加载该用户可以访问的菜单
export const getMenuByUsername = (username) => {
	return jwtInstance.request({
		url: '/user/admin',
		method: 'get',
		params: {
			username: getTokenUser() //从JWT中获取用户名
		}
	})
}

//判断当前用户是否有邮箱
export const emailIsNull = (username) => {
	return jwtInstance.request({
		url: '/user/email',
		method: 'get',
		params: {
			username: username
		}
	})
}

//修改邮箱
export const changeEmail = (email, code) => {
	return jwtInstance.request({
		url: '/user/change',
		method: 'get',
		params: {
			username: getTokenUser(),
			email: email,
			code: code,
		}
	})
}


export const verifyEmail = (email) => {
	return jwtInstance.request({
		url: '/user/verify',
		method: 'get',
		params: {
			email: email
		}
	})
}


export const changePassword = (password) => {
	return jwtInstance.request({
		url: '/user/changepassword',
		method: 'get',
		params: {
			username: getTokenUser(),
			password: password,
		}
	})
}

export const getAsk = (uid, current, size, state) => {
	return jwtInstance.request({
		url: '/ask/asks',
		method: 'get',
		params: {
			uid: uid,
			current: current,
			size: size,
			state: state,
		}
	})
}

export const updateAsk = (id, aid, classroomid, time1, time2, time3, time4, time5, time6) => {
	return jwtInstance.request({
		url: '/ask/updateask',
		method: 'get',
		params: {
			id: id,
			aid: aid,
			classroomid: classroomid,
			time1: time1,
			time2: time2,
			time3: time3,
			time4: time4,
			time5: time5,
			time6: time6,
		}
	})
}

export const notice = (aname, createtime, to, content) => {
	return jwtInstance.request({
		url: '/user/notice',
		method: 'get',
		params: {
			aname: aname,
			createtime: createtime,
			to: to,
			content: content,
		}
	})
}

export const upload = (file) => {
	return jwtInstance.request({
		url: 'user/upload',
		method: 'post',
		data: file,
		headers: {
			'Content-Type': 'multipart/form-data'
		}
	});
}

export const upload2 = (file) => {
	return jwtInstance.request({
		url: 'classroom/upload',
		method: 'post',
		data: file,
		headers: {
			'Content-Type': 'multipart/form-data'
		}
	});
}


export const addNotice = (formData) => {
	return jwtInstance.request({
		url: '/notice/notices',
		method: 'post',
		data: {
			name: formData.name,
			title: formData.title,
			content: formData.content,
		}
	})
}


export const findNotice = (id) => {
	return jwtInstance.request({
		url: '/notice/notice',
		method: 'get',
		params: {
			id: id
		}
	})
}

export const findNotices = () => {
	return jwtInstance.request({
		url: '/notice/notices',
		method: 'get',
	})
}

export const avatar = (file, userid) => {
	return jwtInstance.request({
		url: '/user/avatar?userid=' + userid,
		method: 'post',
		data: file,
		headers: {
			'Content-Type': 'multipart/form-data'
		}
	})
}

export const changeCode = (time) => {
	return jwtInstance.request({
		url: '/kaptcha/kaptcha?d=' + time,
		method: 'get',
	})
}

export const home = (identity) => {
	return jwtInstance.request({
		url: '/user/home',
		method: 'get',
		params: {
			identity: identity,
		}
	})
}


export const send = (chat) => {
	return robot.request({
		url: '/v2',
		method: 'post',
		data: {
			chat: chat,
		},
	})
}

export const getUser = (name) => {
	return jwtInstance.request({
		url: '/user/userid',
		method: 'get',
		params: {
			name: name,
		}
	})
}

export const forget = (code) => {
	return jwtInstance.request({
		url: '/user/forget',
		method: 'get',
		params: {
			code: code,
		}
	})
}

export const changePassword2 = (code, password) => {
	return jwtInstance.request({
		url: '/user/changepassword',
		method: 'get',
		params: {
			username: code,
			password: password,
		}
	})
}


export const cancelAsk = (id,no) => {
	return jwtInstance.request({
		url: '/ask/cancel',
		method: 'get',
		params: {
			id:id,
			no:no,
		}
	})
}

export const chart = (date,time) => {
	return jwtInstance.request({
		url: '/user/chart',
		method: 'get',
		params: {
			date:date,
			time:time,
		}
	})
}
